<?php namespace system\model;

use hdphp\Model\Model;

class Goods extends Model{

	//数据表
	protected $table = "goods";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		['gname','required','请添加商品名称',3,3],
		['gnumber','required','请添加商品号',3,3],
		['unit','required','请添加商品单位',3,3],
		['marketprice','required','请添加市场价',3,3],
		['shopprice','required','请添加商城价',3,3],
		['pic','required','请上传列表图',3,3],
		['click','required','请添加点击量',3,3],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;


	//禁止插入的字段
	protected $denyInsertFields = [];

	//禁止更新的字段
	protected $denyUpdateFields = [];

	//前置方法 比如: _before_add 为添加前执行的方法
	protected function _before_add(){}
	protected function _before_delete(){}
	protected function _before_save(&$data){}

	protected function _after_add(){}
	protected function _after_delete(){}
	protected function _after_save(){}

	//执行添加方法
	public function store(){
		//商品表的验证
		if(!$this->create()) return false;
		//商品详情表的验证
		$goodsDetail=new \system\model\Detail();
		if(!$goodsDetail->create()){
			$this->error=$goodsDetail->getError();
			return false;
		}
		//商品属性表的验证
		$goods_attr=new \system\model\Goodsattr();
		//添加商品表的内容
		//p($this->data);die;
		$this->data['time']=time();
		$this->data['aid']=$_SESSION['admin']['uid'];
		$tid=Db::table('category')->where('cid',$_POST['cid'])->pluck('tid');
		//p($tid);die;
		$this->data['tid']=$tid;
		$gid=$this->add();//执行添加自动返回商品的gid

		//执行商品详情表的添加
		//p($_POST['logo']);die;
		//每一张图都要分成小中大三张图
		$small='';$medium='';$big='';
		foreach($_POST['logo'] as $k=>$v){
			$small .=Image::thumb($v,dirname($v).'/small_'.basename($v),50,50,5).",";
			$medium .=Image::thumb($v,dirname($v).'/medium_'.basename($v),480,480,5).",";
			$big .=Image::thumb($v,dirname($v).'/big_'.basename($v),1000,1000,5).",";
		}
		$small=rtrim($small,',');//去掉最后的一个逗号
		$medium=rtrim($medium,',');
		$big=rtrim($big,',');
		$datad=[
			'small'=>$small,
			'medium'=>$medium,
			'big'=>$big,
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service'],
			'gid'=>$gid,
		];
		//p($datad);die;
		$goodsDetail->add($datad);
		//商品属性表的添加
		//添加属性
		//p($_POST);die;
		foreach($_POST['attr'] as $k=>$v){
			//p($k);die;
			if($v){
				$data=[
					'gavalue'=>$v,
					'added'=>0,
					'taid'=>$k,
					'gid'=>$gid,
				];
				$goods_attr->add($data);
			}
		}
		//添加规格
		foreach($_POST['spec'] as $k=>$v){
			foreach($v['guige'] as $kk=>$vv){
				$data=[
					'gavalue'=>$vv,
					'added'=>$v['added'][$kk],
					'taid'=>$k,
					'gid'=>$gid,
				];
				$goods_attr->add($data);
			}
		}
		return true;
	}
	//编辑
	public function edit(){
		$goodsDetail=new \system\model\Detail();
		$goods_attr=new \system\model\Goodsattr();
		//商品表的修改
		$_POST['time']=time();
		$_POST['aid']=$_SESSION['admin']['uid'];
		$this->save();
		//商品图册处理
		$small='';$medium='';$big='';
		foreach($_POST['logo'] as $k=>$v){
			$small .=Image::thumb($v,dirname($v).'/small_'.basename($v),50,50,5).",";
			$medium .=Image::thumb($v,dirname($v).'/medium_'.basename($v),480,480,5).",";
			$big .=Image::thumb($v,dirname($v).'/big_'.basename($v),1000,1000,5).",";
		}
		$small=rtrim($small,',');//去掉最后的一个逗号
		$medium=rtrim($medium,',');
		$big=rtrim($big,',');
		$datad=[
			'small'=>$small,
			'medium'=>$medium,
			'big'=>$big,
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service'],
			'gid'=>$_POST['gid'],
		];
		//p($datad);die;
		$goodsDetail->where('gid',$_POST['gid'])->update($datad);

		//商品属性表的修改
		//修改属性
		//p($_POST);die;
//		删除表中数据一样的
		Db::table('goods_attr')->where('gid',$_POST['gid'])->delete();
		foreach($_POST['attr'] as $k=>$v){
			//p($k);die;
			if($v){
				$data=[
					'gavalue'=>$v,
					'added'=>0,
					'taid'=>$k,
					'gid'=>$_POST['gid'],
				];
				$goods_attr->add($data);
			}
		}

		//修改规格
		foreach($_POST['spec'] as $k=>$v){
			foreach($v['guige'] as $kk=>$vv){
				$data=[
					'gavalue'=>$vv,
					'added'=>$v['added'][$kk],
					'taid'=>$k,
					'gid'=>$_POST['gid'],
				];
				$goods_attr->add($data);
			}
		}
		return true;
	}
}